init();

function init() {

  initTodoList();
}

var initTodoList = (function () {
  var showInput = document.getElementsByClassName('j-show-input')[0],
    inputWrap = document.getElementsByClassName('input-wrap')[0],
    addItem = document.getElementsByClassName('j-add-item')[0],
    textInput = document.getElementById('textInput'),
    oList = document.getElementsByClassName('j-list')[0],
    inputShow = false,
    isEdit = false,
    curIdx = null;

  addEvent(showInput, 'click', function () {
    if (inputShow) {
      restoreStatue();
      isShowInput('close');
    } else {
      isShowInput("open");
    }
  })

  addEvent(addItem, 'click', function () {
    var oItems = document.getElementsByClassName('item'),
      content = textInput.value,
      contentLen = content.length,
      itemLen = oItems.length;

    if (contentLen === 0) {
      return;
    }

    if (itemLen > 0) {
      for (var i = 0; i < itemLen; i++) {
        var itemText = elemChildren(oItems[i])[0].innerText;

        if (content === itemText) {
          alert('已存在子项目');
          return
        }
      }
    }

    if (isEdit) {
      var itemContent = elemChildren(oItems[curIdx])[0];
      itemContent.innerText = content;
    } else {
      // 创建子项目 li ， 输入框清空
      var oLi = document.createElement('li');
      oLi.className = 'item';
      oLi.innerHTML = itemTpl(content);
      oList.appendChild(oLi);
    }

    isShowInput('close');
    restoreStatue(true);

  })

  // 编辑 和 删除 运用事件代理执行
  // 通过 绑定父级元素的事件，分析事件源元素
  addEvent(oList, 'click', function (e) {
    var e = e || window.event,
      tar = e.target || e.srcElement,
      className = tar.className,
      oParset = elemParent(tar, 2),
      oItems = document.getElementsByClassName('item');

    if (className === 'edit-btn fa fa-edit') {
      // 编辑
      var itemLen = oItems.length,
        tarIndex = Array.prototype.indexOf.call(oItems, oParset),
        item;

      restoreStatue(false);

      for (var i = 0; i < itemLen; i++) {
        item = oItems[i];
        item.className = 'item'
      }

      curIdx = tarIndex

      oParset.className += ' active';
      addItem.innerText = '编辑第' + (curIdx + 1) + '项';
      isEdit = true;

    } else if (className === 'remove-btn fa fa-times') {
      // 删除
      elemParent(tar, 2).remove();
    }
  })

  function restoreStatue() {
    isEdit = false;
    curIdx = null;
    textInput.value = '';
    addItem.innerText = '增加项目';
  }

  function isShowInput(active) {
    if (active === 'close') {
      inputWrap.style.display = 'none';
      inputShow = false;
    } else if (active === 'open') {
      inputWrap.style.display = 'block';
      inputShow = true;
    }

  }

  function itemTpl(text) {
    return (
      '<p class="item-content">' + text + '</p>' +
      '<div class="btn-group">' +
      '<a href="javascript:;" class="edit-btn fa fa-edit"></a>' +
      '<a href="javascript:;" class="remove-btn fa fa-times"></a>' +
      '</div>'
    )
  }
});

var testB = (function () {
  console.log(1)
});
